package com.cly.order.listener;

import com.cly.common.entity.order.po.Order;
import com.cly.common.exception.CommonException;
import com.cly.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RequiredArgsConstructor
public class PayStatusListener {
    private final OrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order.pay.satus.queue", durable = "true"),
            exchange = @Exchange(name = "pay.direct", type =  "direct"),
            key = "pay.status"
    ))
    public void listen(Map< String, Object> message) {
        Long orderId = (Long) message.get("orderId");
        Integer status = (Integer) message.get("status");
        if ( status == 2){
            Order order = orderService.getById(orderId);
            if(order.getStatus() == 1){
                orderService.updateOrderStatus(orderId, status);
            }else{
                throw new CommonException("该订单无法付款！");
            }
        }

    }
}
